<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/3/30
 * Time: 15:27
 */

namespace api\storage\service\client;


use api\storage\service\BaseService;
use service\logger\LoggerFactory;
use think\Collection;

abstract class BaseClientService extends BaseService
{
    protected $logger;

    public function __construct()
    {
        $this->logger = LoggerFactory::getLogger('client');
    }

    /**
     * 日志上传
     *
     * @param $files
     * @param $code
     * @return mixed
     */
    public function uploadLog($files, $code)
    {
        $path = $this->initUploadLogRootPath($code);
        if (is_array($files)){
            foreach ($files as $file){
                $info = $file->move($path);
            }
        }else{
            $info = $files->move($path);
        }
        if ($info) {
            return $this->resultTemplate(1);
        } else {
            $this->logger->info($files->getError());
            return $this->resultTemplate(0, $files->getError());
        }
    }

    /**
     * 版本检测,返回最新版本
     *
     * @param $version
     * @return array
     */
    public function checkVersion($version)
    {
        $data = $this->initVersionData($version);
        if (empty($data) || count($data) == 0) {
            return $this->resultTemplate(0, []);
        }
        //第一个就是最新的数据
        if ($data[0] instanceof Collection){
            $latestVersion = $data[0]->toArray();
        }else{
            $latestVersion = $data[0];
        }
        if (empty($latestVersion['url'])) {
            return $this->resultTemplate(-1, []);
        }
        return $this->resultTemplate(1, $latestVersion);

    }

    /**
     * 获取历史版本
     *
     * @return array
     */
    public function historyVersion(){
        $data = $this->initHistoryData();
        if (empty($data) || count($data) == 0) {
            return $this->resultTemplate(0, []);
        }
        $version = $data->toArray();
        return $this->resultTemplate(1,$version);
    }


    /**
     * 初始化上传日志的路径
     *
     * @param $code
     * @return mixed
     */
    public abstract function initUploadLogRootPath($code);

    /**
     * 初始化版本数据
     *
     * @param $version
     * @return mixed
     */
    public abstract function initVersionData($version);

    /**
     * 获取历史版本数据
     *
     * @return mixed
     */
    public abstract function initHistoryData();
}